<?php

namespace helper;

use think\facade\Db;
use think\facade\Request;
use app\admin\model\sys\AuthRule;

class Menu
{
    //获取左侧主菜单
    public function menu($arr,$pid=0){
        $urules = Db::name('auth_group')->where('id',session('admin.group_id'))->value('rules');
        $authrules = explode(',',$urules);
        $authopen = AuthRule::where('auth_open',1)->column('id');
        if($authopen){
            $authrules = array_unique(array_merge($authrules,$authopen));
        }
        $list =array();
        foreach ($arr as $k=>$v){
            $v['href'] = (string) url($v['href']);
            if (session('admin.id') != 1) {
                if ($v['pid'] == $pid){
                    if(in_array($v['id'],$authrules)){
                        $v['child'] = $this->menu($arr,$v['id']);
                        $list[] = $v;
                    }
                }
            }else{
                if ($v['pid'] == $pid) {
                    $v['child'] = $this->menu($arr, $v['id']);
                    $list[] = $v;
                }
            }

        }

        return $list;

    }

    /*
    * 自定义菜单排列
    */
     public function tree($cate, $lefthtml = '|— ', $pid = 0, $lvl = 0, $leftpin = 0)
     {
         $arr = array();
         foreach ($cate as $v) {
             if ($v['pid'] == $pid) {
                 $v['lvl'] = $lvl + 1;
                 $v['leftpin'] = $leftpin + 0;
                 $v['lefthtml'] = str_repeat($lefthtml, $lvl);
                 $v['ltitle'] = $v['lefthtml'] . $v['title'];
                 $arr[] = $v;
                 $arr = array_merge($arr, $this->tree($cate, $lefthtml, $v['id'], $lvl + 1, $leftpin + 20));
             }
         }

         return $arr;
     }

   // public  static  function cate($cate, $lefthtml = '|— ', $pid = 0, $lvl = 0, $leftpin = 0)
   //  {
   //      $arr = array();
   //      foreach ($cate as $v) {
   //          if ($v['pid'] == $pid) {
   //              $v['lvl'] = $lvl + 1;
   //              $v['leftpin'] = $leftpin + 0;
   //              $v['lefthtml'] = str_repeat($lefthtml, $lvl);
   //              $arr[] = $v;
   //              $arr = array_merge($arr, self::menu($cate, $lefthtml, $v['id'], $lvl + 1, $leftpin + 20));
   //          }
   //      }

   //      return $arr;
   //  }

   // public  static  function auth($cate, $rules, $pid = 0)
   //  {
   //      $arr = array();
   //      $rulesArr = explode(',', $rules);
   //      foreach ($cate as $v) {
   //          if ($v['pid'] == $pid) {
   //              if (in_array($v['id'], $rulesArr)) {
   //                  $v['checked'] = true;
   //              }
   //              $v['open'] = true;
   //              $arr[] = $v;
   //              $arr = array_merge($arr, self::auth($cate, $v['id'], $rules));
   //          }
   //      }
   //      return $arr;
   //  }




}
